/*
 * Copyright (C) 2022 Realtek Semiconductor Corp.
 * All Rights Reserved.
 *
 * This program is the proprietary software of Realtek Semiconductor
 * Corporation and/or its licensors, and only be used, duplicated,
 * modified or distributed under the authorized license from Realtek.
 *
 * ANY USE OF THE SOFTWARE OTHER THAN AS AUTHORIZED UNDER
 * THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
 *
 * Purpose : Definition of ME handler: Extended IP HOST Config Data (65417)
 *
 * Feature : The file includes the following modules and sub-modules
 *           (1) ME handler: Extended IP HOST Config Data (65417)
 */

#include "app_basic.h"
#include "feature_mgmt.h"

MIB_TABLE_INFO_T                gMibMe65417TblInfo;
MIB_ATTR_INFO_T                 gMibMe65417AttrInfo[MIB_TABLE_ME65417_ATTR_NUM];
MIB_TABLE_ME65417_T             gMibMe65417DefRow;
MIB_TABLE_OPER_T                gMibMe65417Oper;

static GOS_ERROR_CODE omci_mib_me65417_oper_reset(UINT32 args_num, ...)
{

    MIB_TABLE_ME65417_T       me65417;
    int id;

    OMCI_LOG(OMCI_LOG_LEVEL_INFO, "Resetting MIB ME 65417");

    for (id = 0; id < TXC_IPHOST_NUM; id++)
    {
        MIB_Default(MIB_TABLE_ME65417_INDEX, &me65417, sizeof(MIB_TABLE_ME65417_T));
        me65417.EntityId = id;
        me65417.Attr9 = 2;
        GOS_ASSERT(GOS_OK == MIB_Set(MIB_TABLE_ME65417_INDEX, &me65417, sizeof(MIB_TABLE_ME65417_T)));
        OMCI_MeOperCfg(MIB_TABLE_ME65417_INDEX, NULL, &me65417, MIB_ADD,
        omci_GetOltAccAttrSet(MIB_TABLE_ME65417_INDEX, OMCI_ME_ATTR_ACCESS_SBC), OMCI_MSG_BASELINE_PRI_LOW);
    }

    return GOS_OK;
}

GOS_ERROR_CODE me65417_drv_cfg_handler(void              *pOldRow,
                                          void            *pNewRow,
                                          MIB_OPERA_TYPE  operationType,
                                          MIB_ATTRS_SET   attrSet,
                                          UINT32          pri)
{

    MIB_TABLE_ME65417_T               *pNewMibMe65417 = NULL,*pOldMibMe65417 = NULL;
    MIB_TABLE_IP_HOST_CFG_DATA_T    mibIpHost;
    mgmt_cfg_msg_t                  mgmtInfo;

    OMCI_LOG(OMCI_LOG_LEVEL_DBG,"%s: me65417  process start \n",__FUNCTION__);

    pOldMibMe65417 = (MIB_TABLE_ME65417_T *)pOldRow;
    pNewMibMe65417 = (MIB_TABLE_ME65417_T *)pNewRow;

    mibIpHost.EntityID = pNewMibMe65417->EntityId;
    if (GOS_OK != MIB_Get(MIB_TABLE_IP_HOST_CFG_DATA_INDEX, &mibIpHost, sizeof(MIB_TABLE_IP_HOST_CFG_DATA_T)))
        return GOS_OK;

    switch (operationType)
    {
        case MIB_SET:
            if(ME65417_IS_PPPOE_EN(pNewMibMe65417->Attr2))
            {
                OMCI_LOG(OMCI_LOG_LEVEL_WARN,"%s: enable PPPoE \n",__FUNCTION__);

                /*Trigger to create interface */
                if(!ME65417_IS_PPPOE_EN(pOldMibMe65417->Attr2))
                    omci_setup_mgmt_interface(OP_SET_IF, IF_CHANNEL_MODE_PPPOE, IF_SERVICE_DATA, &mibIpHost, NULL);

                memset(&mgmtInfo, 0, sizeof(mgmt_cfg_msg_t));
                mgmtInfo.op_id = OP_SET_PPPOE;                
                mgmtInfo.cfg.pppoe.related_if_id = pNewMibMe65417->EntityId;
                mgmtInfo.cfg.pppoe.nat_enabled = TRUE; //TBD
                mgmtInfo.cfg.pppoe.auth_method = 0;
                mgmtInfo.cfg.pppoe.conn_type = 0;
                mgmtInfo.cfg.pppoe.release_time = 0;
                strncpy(mgmtInfo.cfg.pppoe.username, pNewMibMe65417->User, MIB_TABLE_ME65417_STRING_PART_LEN);
                strncpy(mgmtInfo.cfg.pppoe.password, pNewMibMe65417->Password, MIB_TABLE_ME65417_STRING_PART_LEN);
                feature_api(FEATURE_API_L3SVC_MGMT_CFG_SET, &mgmtInfo, sizeof(mgmt_cfg_msg_t));

            }
            else
            {
                 OMCI_LOG(OMCI_LOG_LEVEL_WARN,"%s: disable PPPoE \n",__FUNCTION__);
                // remove pppoe interface for internet and the iphost change to DHCP mode or static mode
                omci_setup_mgmt_interface(OP_RESET_IF, IF_CHANNEL_MODE_PPPOE, IF_SERVICE_DATA, &mibIpHost, NULL);
            }

       default:
            break;
    }

	OMCI_LOG(OMCI_LOG_LEVEL_DBG,"%s: me65417 end\n",__FUNCTION__);

	return GOS_OK;
}

GOS_ERROR_CODE mibTable_init(MIB_TABLE_INDEX tableId)
{
    MIB_ATTR_INDEX  attrIndex;    
    UINT8 proprietary_mib_cb_bitmask = 0;
    UINT8 isIgnore = 0;
    if(GOS_OK == omci_ignore_mib_table_check(65417, OMCI_ME_CLASS_SUB_DEFAULT, &isIgnore))
    {
        if(isIgnore)
        {
            OMCI_PRINT("Ignore classId:%d", 65417);
            return GOS_FAIL;
        }

    }

    gMibMe65417TblInfo.Name = "ME65417";
    gMibMe65417TblInfo.ShortName = "ME65417";
    gMibMe65417TblInfo.Desc = "ME65417";
    gMibMe65417TblInfo.ClassId = 65417;
    gMibMe65417TblInfo.InitType = OMCI_ME_INIT_TYPE_ONU;
    gMibMe65417TblInfo.StdType = OMCI_ME_TYPE_PROPRIETARY;
    gMibMe65417TblInfo.ActionType = OMCI_ME_ACTION_SET | OMCI_ME_ACTION_GET;
    gMibMe65417TblInfo.pAttributes = &(gMibMe65417AttrInfo[0]);
    gMibMe65417TblInfo.attrNum = MIB_TABLE_ME65417_ATTR_NUM;
    gMibMe65417TblInfo.entrySize = sizeof(MIB_TABLE_ME65417_T);
    gMibMe65417TblInfo.pDefaultRow = &gMibMe65417DefRow;

    attrIndex = MIB_TABLE_ME65417_ENTITY_ID_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "EntityId";
    gMibMe65417AttrInfo[attrIndex].Desc = "Entity ID";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_UINT16;
    gMibMe65417AttrInfo[attrIndex].Len = 2;
    gMibMe65417AttrInfo[attrIndex].IsIndex = TRUE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_HEX;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    attrIndex = MIB_TABLE_ME65417_ATTR2_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "attr2";
    gMibMe65417AttrInfo[attrIndex].Desc = "attr2";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_UINT16;
    gMibMe65417AttrInfo[attrIndex].Len = 2;
    gMibMe65417AttrInfo[attrIndex].IsIndex = FALSE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_HEX;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    attrIndex = MIB_TABLE_ME65417_USER_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "user";
    gMibMe65417AttrInfo[attrIndex].Desc = "user";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_STR;
    gMibMe65417AttrInfo[attrIndex].Len = MIB_TABLE_ME65417_STRING_PART_LEN;
    gMibMe65417AttrInfo[attrIndex].IsIndex = FALSE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_CHAR;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    attrIndex = MIB_TABLE_ME65417_ATTR4_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "attr4";
    gMibMe65417AttrInfo[attrIndex].Desc = "attr4";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_UINT16;
    gMibMe65417AttrInfo[attrIndex].Len = 2;
    gMibMe65417AttrInfo[attrIndex].IsIndex = FALSE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_HEX;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    attrIndex = MIB_TABLE_ME65417_ATTR5_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "attr5";
    gMibMe65417AttrInfo[attrIndex].Desc = "attr5";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_UINT16;
    gMibMe65417AttrInfo[attrIndex].Len = 2;
    gMibMe65417AttrInfo[attrIndex].IsIndex = FALSE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_HEX;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    attrIndex = MIB_TABLE_ME65417_PASSWD_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "passwd";
    gMibMe65417AttrInfo[attrIndex].Desc = "passwd";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_STR;
    gMibMe65417AttrInfo[attrIndex].Len = MIB_TABLE_ME65417_STRING_PART_LEN;
    gMibMe65417AttrInfo[attrIndex].IsIndex = FALSE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_CHAR;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;
    
    attrIndex = MIB_TABLE_ME65417_ATTR7_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "attr7";
    gMibMe65417AttrInfo[attrIndex].Desc = "attr7";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_UINT16;
    gMibMe65417AttrInfo[attrIndex].Len = 2;
    gMibMe65417AttrInfo[attrIndex].IsIndex = FALSE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_HEX;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    attrIndex = MIB_TABLE_ME65417_ATTR8_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "attr8";
    gMibMe65417AttrInfo[attrIndex].Desc = "attr8";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_STR;
    gMibMe65417AttrInfo[attrIndex].Len = MIB_TABLE_ME65417_STRING_PART_LEN;
    gMibMe65417AttrInfo[attrIndex].IsIndex = FALSE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_HEX;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    attrIndex = MIB_TABLE_ME65417_ATTR9_INDEX - MIB_TABLE_FIRST_INDEX;
    gMibMe65417AttrInfo[attrIndex].Name = "attr9";
    gMibMe65417AttrInfo[attrIndex].Desc = "attr9";
    gMibMe65417AttrInfo[attrIndex].DataType = MIB_ATTR_TYPE_UINT8;
    gMibMe65417AttrInfo[attrIndex].Len = 1;
    gMibMe65417AttrInfo[attrIndex].IsIndex = FALSE;
    gMibMe65417AttrInfo[attrIndex].MibSave = TRUE;
    gMibMe65417AttrInfo[attrIndex].OutStyle = MIB_ATTR_OUT_HEX;
    gMibMe65417AttrInfo[attrIndex].OltAcc = OMCI_ME_ATTR_ACCESS_READ | OMCI_ME_ATTR_ACCESS_WRITE;
    gMibMe65417AttrInfo[attrIndex].AvcFlag = FALSE;
    gMibMe65417AttrInfo[attrIndex].OptionType = OMCI_ME_ATTR_TYPE_MANDATORY;

    memset(&gMibMe65417DefRow, 0x00, sizeof(gMibMe65417DefRow));
  
    memset(&gMibMe65417Oper, 0x0, sizeof(MIB_TABLE_OPER_T));
    gMibMe65417Oper.meOperDrvCfg = me65417_drv_cfg_handler;
    gMibMe65417Oper.meOperConnCfg = NULL;
    gMibMe65417Oper.meOperConnCheck = NULL;
    gMibMe65417Oper.meOperDump = omci_mib_oper_dump_default_handler;
    gMibMe65417Oper.meOperCb[PROPRIETARY_MIB_CB_RESET] = omci_mib_me65417_oper_reset;

    MIB_TABLE_ME65417_INDEX = tableId;
    MIB_InfoRegister(tableId, &gMibMe65417TblInfo, &gMibMe65417Oper);
    
    proprietary_mib_cb_bitmask = (1 << PROPRIETARY_MIB_CB_RESET);
    MIB_Proprietary_Reg(tableId, proprietary_mib_cb_bitmask);

    return GOS_OK;
}
